package edu.swust.graduation_project.eauction.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.*
import com.bumptech.glide.Glide
import edu.swust.graduation_project.eauction.R
import java.sql.Connection
import java.sql.DriverManager

class OrderEditActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_order)
        val mIvOrder = findViewById<ImageView>(R.id.iv_order_icon)
        val mTvOrderName = findViewById<TextView>(R.id.tv_order_name)
        val mEtOrderMessage = findViewById<EditText>(R.id.et_order_message)
        val mEtOrderPhone = findViewById<EditText>(R.id.et_order_phone)
        val mBtnOrder = findViewById<Button>(R.id.btn_order)

        val buyer = intent.getStringExtra("buyer")
        val itemCreateTime = intent.getSerializableExtra("item_create_time").toString()
        var buyerPhone = intent.getStringExtra("buyerPhone")
        val itemName = intent.getStringExtra("itemName")
        var buyerAddress = intent.getStringExtra("buyerAddress")
        val seller = intent.getStringExtra("seller")
        var sellerPhone = intent.getStringExtra("sellerPhone")
        var sellerExpressNumber = intent.getStringExtra("seller_express_number")
        val isBuy = intent.getBooleanExtra("isBuy", true)
        Log.d("itemCreateTime",itemCreateTime)
        Glide.with(this)
            .load(
                "*" +
                        itemCreateTime.trim().replace(" ", "%20")
                            .replace(":", "%3A").replace(".0", "") +
                        ".png"
            )
            .into(mIvOrder)
        mTvOrderName.text=itemName


        //买家和卖家共用一个Activity，根据属性来填写信息
        if (isBuy) {
            mEtOrderMessage.hint = "请输入您的地址"
            mBtnOrder.setOnClickListener {
                if(mEtOrderMessage.text.isNullOrBlank()||mEtOrderPhone.text.isNullOrBlank()){
                    Toast.makeText(this,"请完成以上信息后再提交订单",Toast.LENGTH_SHORT).show()
                }else{
                    setBuyerMysql(mEtOrderMessage.text.toString(),mEtOrderPhone.text.toString(),itemCreateTime)
                }
            }
        }else{
            mEtOrderMessage.hint = "请输入快递单号"
            mBtnOrder.setOnClickListener {
                if(mEtOrderMessage.text.isNullOrBlank()||mEtOrderPhone.text.isNullOrBlank()){
                    Toast.makeText(this,"请完成以上信息后再提交订单",Toast.LENGTH_SHORT).show()
                }else{
                    setSellerMysql(mEtOrderMessage.text.toString(),mEtOrderPhone.text.toString(),itemCreateTime)
                }
            }
        }
    }

   private fun setBuyerMysql(buyerAddress: String?, buyerPhone: String?, itemCreateTime: String) {
       object :Thread(){
           override fun run() {
               super.run()
               val UpdateSQL="UPDATE `test_case`.`Delivery` SET " +
                       "`buyer_address` = '$buyerAddress', " +
                       "`buyer_phone` = '$buyerPhone' " +
                       "WHERE `Delivery`.`item_create_time` = '$itemCreateTime'"
               val cn: Connection = DriverManager.getConnection(
                   "*",
                   "*", "*"
               )
               val ps = cn.createStatement()
               ps.execute(UpdateSQL)
               ps.close()
               cn.close()
               finish()
           }
       }.start()
    }

    private fun setSellerMysql(sellererExpress: String?, sellerPhone: String?, itemCreateTime: String) {
        object :Thread(){
            override fun run() {
                super.run()
                val UpdateSQL="UPDATE `test_case`.`Delivery` SET " +
                        "`seller_express_number` = '$sellererExpress', " +
                        "`seller_phone` = '$sellerPhone' " +
                        "WHERE `Delivery`.`item_create_time` = '$itemCreateTime'"
                val cn: Connection = DriverManager.getConnection(
                    "*",
                    "*", "*"
                )
                val ps = cn.createStatement()
                ps.execute(UpdateSQL)
                ps.close()
                cn.close()
                finish()
            }
        }.start()
    }
}